﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using FwProject.Comm;
using FwProject.Core.Distribution;
using FwProject.Core.Restaurant;
using FwProject.Distribution;
using FwProject.Distribution.Dto;

using FwProject.Manager;
using FwProject.PublicService.JPush;
using FwProject.Restaurant;
using FwProject.Users;
using Microsoft.EntityFrameworkCore;

namespace FwProject.RemoteEventBus
{
#if DEBUG
    [RemoteEventHandler(ForType = "RemoteTest", ForTopic = nameof(DistributionDetailsMessage))]
#endif
    public class DistributionDetailsMessage : IRemoteEventHandler, ITransientDependency
    {
        public readonly ICacheManager _catchmanager;
        public readonly IJPushAppservice _jPushAppservice;
        public readonly IUserAppService _userAppService;
        public readonly IRepository<DistributionInfo, Guid> _repositoryDistributionInfo;
        public readonly IRepository<DistributionInfoDetails, Guid> _repositoryDistributionInfoDetails;
        private readonly LoadCarRecoveryManager _loadCarRecoveryManager;
        private readonly LoadingCarManager _loadingCarManager;
        public IRemoteEventBus remoteEventBus { get; set; }//缓存对象
        public IGuidGenerator _guid { get; set; }//guid 对象

        private readonly IUnitOfWorkManager _unitOfWorkManager;
        public DistributionDetailsMessage(ICacheManager catchmanager, IJPushAppservice jPushAppservice,
                                          IUserAppService userAppService,
                                          IRepository<DistributionInfo, Guid> repositoryDistributionInfo,
                                           LoadCarRecoveryManager loadCarRecoveryManager,
                                           LoadingCarManager loadingCarManager,
                                           IRepository<DistributionInfoDetails, Guid> repositoryDistributionInfoDetails,
                                           IUnitOfWorkManager unitOfWorkManager
                                       )
        {
            remoteEventBus = NullRemoteEventBus.Instance;
            _loadingCarManager = loadingCarManager;
            _loadCarRecoveryManager = loadCarRecoveryManager;
            _repositoryDistributionInfo = repositoryDistributionInfo;
            _catchmanager = catchmanager;
            _jPushAppservice = jPushAppservice;
            _repositoryDistributionInfoDetails = repositoryDistributionInfoDetails;
            _userAppService = userAppService;
            _unitOfWorkManager = unitOfWorkManager;

        }
        [UnitOfWork]
        public void HandleEvent(RemoteEventArgs eventArgs)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
            {
                var t = _repositoryDistributionInfoDetails.GetAll();
                var detailsModel = t.FirstOrDefault();
                var dtModl = _repositoryDistributionInfo.GetAll().Where(m => m.Id == detailsModel.DistributionInfoId).FirstOrDefault();
                var loadingModel = _loadingCarManager.GetByTypeNumberId(detailsModel.TypeNumberInfoId.Value, dtModl.LoadCarId.Value);
                if (loadingModel == null)
                {
                   
                }

                unitOfWork.Complete();
            }
        }
    }

}
